package com.cskaoyan.wordmemorize.controller.admin;


import com.cskaoyan.wordmemorize.aop_whh.annotation.OperateLog;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteRolePermissionOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.RoleDTO;
import com.cskaoyan.wordmemorize.dto.admin.RolePermissionDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.RoleCommand;
import com.cskaoyan.wordmemorize.request.RolePermissionCommand;
import com.cskaoyan.wordmemorize.service.RolePermissionService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin")
public class RoleController {
    @Resource
    RolePermissionService rolePermissionService;
    //获取账号角色列表
    @RequestMapping("role/list")
    public Result<PageDTO<RoleDTO>> getRoleList(PageRequest request) {
        PageDTO<RoleDTO> roleList = rolePermissionService.getRoleList(request);
        return Result.ok(roleList);
    }
    //添加角色
    @PostMapping("role")
    @OperateLog(dateType = "角色",operateType = 1)
    public Result addRole(@RequestBody RoleCommand role) {
        rolePermissionService.addRole(role);
        return Result.ok();
    }
    //修改角色
    @PutMapping("role")
    @OperateLog(dateType = "角色",operateType = 4)
    public Result updateRole(@RequestBody RoleCommand role) {
        rolePermissionService.updateRole(role);
        return Result.ok();
    }
    //删除角色
    @DeleteMapping("role/{roleId}")
    @OperateLog(dateType = "角色",operateType = 2)
    public Result<String> deleteRole(@PathVariable Long roleId) {
        rolePermissionService.deleteRole(roleId);
        return Result.ok();
    }

    //获取角色对应的授权信息
    @RequestMapping("{roleId}/permission/list")
    public Result<RolePermissionDTO> getRolePermissionList(@PathVariable Long roleId) {
        RolePermissionDTO rolePermissionDTO = rolePermissionService.getRolePermissionList(roleId);
        return Result.ok(rolePermissionDTO);
    }

    //在给角色授权时，删除指定角色绑定的指定权限
    @DeleteMapping("permission/{roleId}/{permissionId}")
    @OperateLog(dateType = "角色",operateType = 2)
    public Result<String> deleteRolePermission(@PathVariable Long roleId, @PathVariable Long permissionId) {
        rolePermissionService.deleteRolePermission(roleId, permissionId);
        return Result.ok();
    }
    //账号角色获权
    @PutMapping("role/permission")
    public Result<String> updateRolePermission(@RequestBody RolePermissionCommand command) {
        rolePermissionService.updateRolePermission(command);
        return Result.ok();
    }

}
